/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.domain.assessment;

import cassys.domain.classManagement.Module;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author User
 */
public class Assessment implements Serializable {

    private Long assessmentId;
    private String title;
    private String description;
    private int maxMark;
    private MarkRange markRange;
    private Collection<AssessmentItem> assessmentItems;
    private Module module;
    private double weightPercentage;

    public boolean validateMarkRange(double mark) throws IllegalMarkException {
        if (mark > this.markRange.getMax()) {
            throw new IllegalMarkException("Mark for this assessment should not be greater than " + this.markRange.getMax());
        } else if (mark < this.markRange.getMin()) {
            throw new IllegalMarkException("Mark for this assessment should not be less than " + this.markRange.getMin());
        }

        return true;
    }

    public Assessment() {
        maxMark = 100;
        markRange = new MarkRange(10, 0);
        assessmentItems = new ArrayList<AssessmentItem>();
    }

    public Assessment(Module module, String title, double weight) {
        this();
        this.module = module;
        module.addAssessment(this);
        this.title = title;
        this.weightPercentage = weight;
    }

    public double evaluate(Collection<EvaluationItem> evaluationItems) {
        double mark = 0.0;
        for (EvaluationItem evaluationItem : evaluationItems) {
            mark = mark + evaluationItem.evaluate();
        }
        return mark * (this.getWeightPercentage() / 100);
    }

    public void addAccessmentItem(AssessmentItem accessmentItem) {
        this.assessmentItems.add(accessmentItem);
    }

    public int getMaxMark() {
        return maxMark;
    }

    public void setMaxMark(int maxMark) {
        this.maxMark = maxMark;
    }

    public MarkRange getMarkRange() {
        return markRange;
    }

    public void setMarkRange(MarkRange markRange) {
        this.markRange = markRange;
    }

    public Collection<AssessmentItem> getAccessmentItems() {
        return assessmentItems;
    }

//    void markAccessmentItem(AssessmentItem markItem, int mark) throws IllegalMarkException {
//        if (mark > this.markRange.getMax()) {
//            throw new IllegalMarkException("Mark for this assessment should not be greater than " + this.markRange.getMax());
//        } else if (mark < this.markRange.getMin()) {
//            throw new IllegalMarkException("Mark for this assessment should not be less than " + this.markRange.getMin());
//        }
//
//
//        for (AssessmentItem assessmentItem : assessmentItems) {
//            if (assessmentItem.equals(markItem)) {
//                assessmentItem.setMark(mark);
//            }
//        }
//    }

    /**
     * @return the moduleId
     */
    public Long getAssessmentId() {
        return assessmentId;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the module
     */
    public Module getModule() {
        return module;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @return the weightPercentage
     */
    public double getWeightPercentage() {
        return weightPercentage;
    }

    /**
     * @param weightPercentage the weightPercentage to set
     */
    public void setWeightPercentage(double weightPercentage) {
        this.weightPercentage = weightPercentage;
    }
}
